﻿Public Class FormProductEditor


    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

        If txtProductCode.Text = "" Then
            MsgBox("Please input product code!")
            txtProductCode.Focus()
            Exit Sub
        End If

        If txtProductId.Enabled Then
            Dim count = Sql.ExecuteScalar("SELECT COUNT(*) FROM Product WHERE ProductCode='" & txtProductCode.Text & "' AND Active=1")
            If (count > 0) Then
                MsgBox("Product code is already exists!")
                txtProductCode.Focus()
                Exit Sub
            End If
        Else
            Dim count = Sql.ExecuteScalar("SELECT COUNT(*) FROM Product WHERE ProductCode='" & txtProductCode.Text & "' AND ProductId <> " & txtProductId.Text & " AND Active=1")
            If (count > 0) Then
                MsgBox("Product code is already exists!")
                txtProductCode.Focus()
                Exit Sub
            End If
        End If
        


        If txtProductName.Text = "" Then
            MsgBox("Please input product name!")
            txtProductName.Focus()
            Exit Sub
        End If


        If txtPrice.Text = "" Then
            MsgBox("Please input price!")
            txtPrice.Focus()
            Exit Sub
        End If

        If Validation.IsDecimal(txtPrice.Text) = False Then
            MsgBox("Please input a correct decimal value!")
            txtPrice.Focus()
            Exit Sub
        End If

        Dim price = txtPrice.Text
        If price <= 0 Or price >= 5000 Then
            MsgBox("Please input a correct price from 0 to 5000$!")
            txtPrice.Focus()
            Exit Sub
        End If


        If txtMinimumPrice.Text = "" Then
            MsgBox("Please input minimum price!")
            txtMinimumPrice.Focus()
            Exit Sub
        End If

        If Validation.IsDecimal(txtMinimumPrice.Text) = False Then
            MsgBox("Please input a correct decimal value!")
            txtMinimumPrice.Focus()
            Exit Sub
        End If

        Dim minprice = txtMinimumPrice.Text
        If minprice <= 0 Or minprice >= 5000 Then
            MsgBox("Please input a correct minimum price from 0 to 5000$!")
            txtMinimumPrice.Focus()
            Exit Sub
        End If

        Dim CategoryName = Me.cboCategoryName.SelectedValue
        If txtProductId.Enabled Then
            Sql.ExecuteNonQuery("INSERT INTO Product VALUES(N'" & txtProductCode.Text & "',N'" & txtProductName.Text & "'," & CategoryName & "," & txtPrice.Text & "," & txtMinimumPrice.Text & ",1)")
        Else
            Sql.ExecuteNonQuery("UPDATE Product SET ProductCode=N'" & txtProductCode.Text & "',ProductName=N'" & txtProductName.Text & "',CategoryId=" & CategoryName & ",Price=" & txtPrice.Text & ",MinimumPrice=" & txtMinimumPrice.Text & " WHERE ProductId=" & txtProductId.Text & "")
        End If

        Me.DialogResult = Windows.Forms.DialogResult.OK

    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        cboTypeName.ValueMember = "TypeId"
        cboTypeName.DisplayMember = "TypeName"
        cboTypeName.DataSource = Sql.ExecuteDataTable("SELECT * FROM Type WHERE Active=1")

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub cboTypeName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTypeName.SelectedIndexChanged

        If Not cboTypeName.SelectedValue Is Nothing Then
            cboCategoryName.ValueMember = "CategoryId"
            cboCategoryName.DisplayMember = "CategoryName"
            cboCategoryName.DataSource = Sql.ExecuteDataTable("SELECT * FROM Category WHERE Active=1 AND TypeId=" & cboTypeName.SelectedValue)
        End If
    End Sub
End Class